package presenter.duplicate;

import main.InputBean;
import utils.*;

import java.io.File;
import java.util.*;

public class FileSizer {
    private final String path1;
    private final String path2;
    private long start;
    private final OnLogListener listener;
    private final List<FileBean> items = new ArrayList<>();
    private final Map<String, List<FileBean>> filters = new HashMap<>();

    public static void parse(InputBean info, OnLogListener listener) {
        new FileSizer(info.path1(), info.path2(), listener);
    }

    private FileSizer(String path1, String path2, OnLogListener listener) {
        this.path1 = path1;
        this.path2 = path2;
        this.listener = listener;
        check(() -> filter(() -> concat(() -> print(this::end))));
    }

    private void check(Runnable next) {
        log("任务开始！");
        this.start = time();
        if (TextUtils.isEmpty(path1)) {
            log("地址存在空！");
            listener.taskFinish();
        } else {
            File targetDirs = new File(path1);
            if (!targetDirs.isDirectory()) {
                log("地址不是文件夹！");
                listener.taskFinish();
            } else {
                next.run();
            }
        }
    }

    private void filter(Runnable next) {
        work(() -> {
            log("开始过滤！文件分析中，时间可能较长，请稍等...");
            List<FileBean> targets = FileFilter.filter(true, path1, null);
            if (targets == null) {
                targets = new ArrayList<>();
            }
            List<FileBean> deletes = TextUtils.isEmpty(path2) ? new ArrayList<>() : FileFilter.filter(false, path2, null);
            if (deletes == null) {
                deletes = new ArrayList<>();
            }
            log(String.format("共分析过滤%d个文件！", (targets.size() + deletes.size())));
            items.addAll(targets);
            items.addAll(deletes);
            if (items.isEmpty()) {
                listener.taskFinish();
            } else {
                next.run();
            }
        });
    }

    private void concat(Runnable next) {
        log("开始大小分区");
        for (FileBean item : items) {
            String size = item.key();
            if (TextUtils.equals(size, "0")) {
                continue;
            }
            List<FileBean> group = filters.get(size);
            if (group == null) {
                group = new ArrayList<>();
                filters.put(size, group);
            }
            group.add(item);
        }
        log("文件大小区分完毕！");
        items.clear();
        next.run();
    }

    private void print(Runnable next) {
        Set<Map.Entry<String, List<FileBean>>> entries = filters.entrySet();
        Iterator<Map.Entry<String, List<FileBean>>> iterator = entries.iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, List<FileBean>> entry = iterator.next();
            String key = entry.getKey();
            List<FileBean> group = entry.getValue();
            if (group != null && group.size() >= 2) {
                log(String.format("\n--------------->大小为%s的存在%d个文件，需自行过滤！<---------------", key, group.size()));
                for (FileBean item : group) {
                    log(item.path());
                }
            }
        }
        log("打印完毕！");
        next.run();
    }

    private void end() {
        long end = time();
        log(String.format("----------任务结束！！！共用时%d秒----------", (int) (end - start)));
        listener.taskFinish();
    }

    private void work(Runnable action) {
        ThreadPool.work(action);
    }

    private long time() {
        return System.currentTimeMillis() / 1000;
    }

    private void log(String content) {
        listener.log(content);
    }
}
